package com.kmxd.ams.app.archive;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.archive.IArchiveVersionSvc;
import com.kmxd.ams.client.archive.dto.ArchiveVersionAddDTO;
import com.kmxd.ams.client.archive.dto.ArchiveVersionEditDTO;
import com.kmxd.ams.client.archive.dto.ArchiveVersionQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveVersionListVO;
import com.kmxd.ams.client.archive.vo.ArchiveVersionVO;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.core.util.SecurityUtil;
import com.kmxd.ams.infra.archive.entity.ArchiveVersion;
import com.kmxd.ams.infra.archive.mapper.ArchiveVersionMapper;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 版本表管理 服务实现类
 *
 * @author nickbi
 * @since 2022-07-19
 */
@Service
@AllArgsConstructor
public class ArchiveVersionSvcImpl extends ServiceImpl<ArchiveVersionMapper, ArchiveVersion>
    implements IArchiveVersionSvc {

  private ArchiveVersionMapper archiveVersionMapper;

  @Override
  public Page<ArchiveVersionListVO> selectPage(Page reqPage, ArchiveVersionQueryDTO req) {
    LambdaQueryWrapper<ArchiveVersion> queryWrapper =
        QueryGen.init(new QueryWrapper<ArchiveVersion>(), req).lambda();
    Page<ArchiveVersion> page = archiveVersionMapper.selectPage(reqPage, queryWrapper);
    Page<ArchiveVersionListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), ArchiveVersionListVO.class));
    return voPage;
  }

  @Override
  public List<ArchiveVersionListVO> selectList(ArchiveVersionQueryDTO req) {
    LambdaQueryWrapper<ArchiveVersion> queryWrapper =
        QueryGen.init(new QueryWrapper<ArchiveVersion>(), req).lambda();
    List<ArchiveVersion> list = archiveVersionMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, ArchiveVersionListVO.class);
  }

  @Override
  public List<ArchiveVersionListVO> selectListAll() {
    LambdaQueryWrapper<ArchiveVersion> queryWrapper = Wrappers.lambdaQuery();
    List<ArchiveVersion> list = archiveVersionMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, ArchiveVersionListVO.class);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(ArchiveVersionAddDTO req) {
    req.setFondsId(
        ObjectUtil.isNotEmpty(req.getFondsId()) ? req.getFondsId() : SecurityUtil.getFondsId());
    ArchiveVersion entity = BeanUtil.copyProperties(req, ArchiveVersion.class);
    archiveVersionMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(ArchiveVersionEditDTO req) {
    ArchiveVersion entity = BeanUtil.copyProperties(req, ArchiveVersion.class);
    archiveVersionMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public ArchiveVersionVO view(Long id) {
    ArchiveVersion entity = archiveVersionMapper.selectById(id);
    return BeanUtil.copyProperties(entity, ArchiveVersionVO.class);
  }
}
